
#include "CGamePauseScreen.h"
#include "CHelpScreen.h"
#include "CSettingsScreen.h"
#include "CBackgroundScreen.h"

void CGamePauseScreen::load(CGraphics *gl)
{
    exitConfirmationScreen = false;
    exitSelection = 0;
    selection = 0;
    font = QFont("Verdana", 15, 0, 0 );

    labels.append( TMLabel(QString::fromStdWString(L"Pauzė"), 300, 120, font, -1));
    buttons.append( TMLabel(QString::fromStdWString(L"Grįžti į žaidimą"), 300, 150, font, 0));
    buttons.append( TMLabel(QString::fromStdWString(L"Baigti žaidimą"), 300, 170, font, 1));
    buttons.append( TMLabel(QString::fromStdWString(L"Nustatymai"), 300, 190, font, 2));
    buttons.append( TMLabel(QString::fromStdWString(L"Pagalba"), 300, 210, font, 3));

    exitLabels.append( TMLabel(QString::fromStdWString(L"Ar tikrai norite baigti šį žaidimą?"), 200, 250, font, -1) );
    exitButtons.append( TMLabel(QString::fromStdWString(L"Taip"), 300, 300, font, 0));
    exitButtons.append( TMLabel(QString::fromStdWString(L"Ne"), 400, 300, font, 1));
}

void CGamePauseScreen::unload(CGraphics *gl)
{

}

int CGamePauseScreen::update(CInput *in)
{
    int action = -1;

    if ( exitConfirmationScreen ) {        
        if ( in->wasKeyPressed(Qt::Key_Escape) )
            action = 1;
        if ( in->wasKeyPressed(Qt::Key_Return) )    // Enter
            action = exitSelection;

        if ( in->wasButtonPressed(Qt::LeftButton) )    // pelės paspaudimas
            foreach ( TMLabel l, exitButtons ) {
                int Mx = in->getCursorPos().x();
                int My = in->getCursorPos().y();
                if ( l.x < Mx && Mx < l.x2 && l.y2+10 < My && My < l.y+10 )
                    action = l.action;
            }

        if ( in->hasMouseMoved() )           // pelės žymėjimas
            foreach ( TMLabel l, exitButtons ) {
                int Mx = in->getCursorPos().x();
                int My = in->getCursorPos().y();
                if ( l.x < Mx && Mx < l.x2 && l.y2+10 < My && My < l.y+10 ) {
                    if ( l.action != exitSelection ) {
                        sm->getSound()->play( "audio/opr000L0-6.mp3", 0.5 );
                        exitSelection = l.action;
                    }
                }
            }

        if( (in->wasKeyPressed(Qt::Key_Down) || in->wasKeyPressed(Qt::Key_Right) )
            && exitSelection < exitButtons.size()-1 ) {  //  žymėjimas klaviatūra
            sm->getSound()->play( "audio/opr000L0-6.mp3", 0.5 );
            exitSelection++;
        } else if( (in->wasKeyPressed(Qt::Key_Up) || in->wasKeyPressed(Qt::Key_Left) )
            && exitSelection > 0 ) {
            sm->getSound()->play( "audio/opr000L0-6.mp3", 0.5 );
            exitSelection--;
        }

        if ( action == 0 ) {    // Taip
            sm->addScreen( new CBackgroundScreen("backgroundScreen"));
            sm->addScreen( new CMenuScreen("menuScreen") );
            sm->removeScreen("testScreen");
            sm->removeScreen(this);
        } else if ( action == 1 ) { // Ne
            exitConfirmationScreen = false;
            exitSelection = 0;
        }
    } else {

        if ( in->wasKeyPressed(Qt::Key_Escape) )
            action = 0;
        if ( in->wasKeyPressed(Qt::Key_Return) )    // Enter
            action = selection;

        if ( in->wasButtonPressed(Qt::LeftButton) )    // pelės paspaudimas
            foreach ( TMLabel l, buttons ) {
                int Mx = in->getCursorPos().x();
                int My = in->getCursorPos().y();
                if ( l.x < Mx && Mx < l.x2 && l.y2+10 < My && My < l.y+10 )
                    action = l.action;
            }

        if ( in->hasMouseMoved() )           // pelės žymėjimas
            foreach ( TMLabel l, buttons ) {
                int Mx = in->getCursorPos().x();
                int My = in->getCursorPos().y();
                if ( l.x < Mx && Mx < l.x2 && l.y2+10 < My && My < l.y+10 ) {
                    if ( selection != l.action ) {
                        sm->getSound()->play( "audio/opr000L0-6.mp3", 0.5 );
                        selection = l.action;
                    }
                }
            }

        if( in->wasKeyPressed(Qt::Key_Down) && selection < buttons.size()-1 ) {  //  žymėjimas klaviatūra
            selection++;
            sm->getSound()->play( "audio/opr000L0-6.mp3", 0.5 );
        } else if( in->wasKeyPressed(Qt::Key_Up) && selection > 0 ) {
            selection--;
            sm->getSound()->play( "audio/opr000L0-6.mp3", 0.5 );
        }

        if ( action == 0 ) {    // Grįžti į žaidimą
            sm->removeScreen(this);
        } else if ( action == 1 ) { // Baigti žaidimą
            exitConfirmationScreen = true;
        } else if ( action == 2 ) { // Nustatymai
            sm->addScreen( new CSettingsScreen("settingsScreen") );
            this->setHidden(true);
        } else if ( action == 3 ) { // Pagalba
            sm->addScreen( new CHelpScreen("helpScreen") );
            CScreen* sc;
//            sc = sm->getScreen("testScreen");
//            if ( sc != 0 ) sc->setHidden(true);
            sc = sm->getScreen("gamePauseScreen");
            if ( sc != 0 ) sc->setHidden(true);
        } else if ( action == 4 ) { // Grįžti į žaidimą
        }
    }

    for ( int i = 0; i < buttons.size(); i++ ) {
        if ( i == selection ) {
            buttons[i].fade += 0.2;
            if ( buttons[i].fade > 2.0 ) buttons[i].fade = 2.0;
        } else {
            if ( buttons[i].fade > 0.0 ) {
                buttons[i].fade -= 0.1;
                if ( buttons[i].fade < 0.0 ) buttons[i].fade = 0.0;
            }
        }
    }
    for ( int i = 0; i < exitButtons.size(); i++ ) {
        if ( i == exitSelection ) {
            exitButtons[i].fade += 0.2;
            if ( exitButtons[i].fade > 2.0 ) exitButtons[i].fade = 2.0;
        } else {
            if ( exitButtons[i].fade > 0.0 ) {
                exitButtons[i].fade -= 0.1;
                if ( exitButtons[i].fade < 0.0 ) exitButtons[i].fade = 0.0;
            }
        }
    }

    return 0;
}

void CGamePauseScreen::draw(CGraphics *gl)
{
    glDisable(GL_TEXTURE_2D);       // piešiamas pusiau permatomas juodas ekranas
    glColor4f(0.0f, 0.0f, 0.0f, 0.5f );
    glBegin(GL_QUADS);  // fonas
        glVertex2i(  0, 0 );
        glVertex2i(  0, 500 );
        glVertex2i(700, 500 );
        glVertex2i(700, 0 );
    glEnd();
    glEnable(GL_TEXTURE_2D);

    if ( exitConfirmationScreen ) {
        glColor3f(1.0f, 1.0f, 1.0f );        
        foreach ( TMLabel l, exitLabels ) { // tekstukai
            gl->renderTexts( l.x, l.y, l.text, font );
        }
        foreach ( TMLabel l, exitButtons ) { // mygtukai
            glColor4f( 1.0f, 1.0f, 1.0f, 0.5f+0.25*l.fade );
            gl->renderTexts( l.x, l.y, l.text, font );
        }
    } else {
        glColor3f(1.0f, 1.0f, 1.0f );
        foreach ( TMLabel l, labels ) { // tekstukai
            gl->renderTexts( l.x, l.y, l.text, font );
        }
        foreach ( TMLabel l, buttons ) { // mygtukai
            glColor4f( 1.0f, 1.0f, 1.0f, 0.5f+0.25*l.fade );
            gl->renderTexts( l.x, l.y, l.text, font );
        }
    }
}
